﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.IO;
using KegData;

namespace KegManager
{
    /// <summary>
    /// Interaction logic for BeerWindow.xaml
    /// </summary>
    public partial class BeerWindow : Window
    {
        public KegeratorDataContext monitor { get; set; }
        public Beer beer { get; set; }
        public bool IsNew = false;

        public BeerWindow(KegeratorDataContext m)
            : this(m, null)
        {
        }

        public BeerWindow(KegeratorDataContext m, Beer b)
        {
            monitor = m;

            if(b == null)
            {
                b = new Beer();
                IsNew = true;
            }
            else
            {
                IsNew = false;
            }

            beer = b;

            InitializeComponent();
        }

        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (RatingText != null)
            {
                if (e.NewValue == 1)
                {
                    RatingText.Text = "Dog Pee";
                }
                else if (e.NewValue == 2)
                {
                    RatingText.Text = "Won't Re-make ";
                }
                else if (e.NewValue == 3)
                {
                    RatingText.Text = "Like It";
                }
                else if (e.NewValue == 4)
                {
                    RatingText.Text = "Great";
                }
                else if (e.NewValue == 5)
                {
                    RatingText.Text = "Love It";
                }
            }
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;

            // Undo any changes to the object
            // Only do this is cancel was used on an edited object.
            // If the object is new.
            if (monitor.Beers.Contains(beer))
            {
                monitor.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, beer);
            }
            

            this.Close();
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;

            // Add this entry to the data source
            if (IsNew)
            {
                monitor.Beers.InsertOnSubmit(beer);
            }

            monitor.SubmitChanges();

            this.Close();
        }

        private void buttonBrowseLabel_Click(object sender, RoutedEventArgs e)
        {
            // Bring up a file dialog
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            dlg.Filter = "Pictures (*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png|" +
                         "All Files (*.*)|*.*";
            dlg.FilterIndex = 1;
            dlg.Multiselect = false;

            bool? FileSelected = dlg.ShowDialog(this);
            if(FileSelected == true)
            {
                if(!Directory.Exists(Properties.Settings.Default.LabelDirectory))
                {
                    Directory.CreateDirectory(Properties.Settings.Default.LabelDirectory);
                }
                
                String FileName = System.IO.Path.GetFileName(dlg.FileName);
                String NewFileLocation = System.IO.Path.Combine(Properties.Settings.Default.LabelDirectory, FileName);

                MessageBoxResult Result = MessageBoxResult.Yes;
                if(System.IO.File.Exists(NewFileLocation))
                {
                    Result = MessageBox.Show("A label by this name already exists.  Would you like to overwrite it?", 
                        "Label Exists",
                        MessageBoxButton.YesNoCancel);

                    if(Result == MessageBoxResult.No || Result == MessageBoxResult.Cancel)
                    {
                        return;
                    }
                }

                // TODO: This throws an exceptopn if it tries to overwrite a label that is in use.
                try
                {
                    System.IO.File.Copy(dlg.FileName, NewFileLocation, true);
                }
                catch (Exception)
                {
                }
                beer.Label = FileName;
            }
        }

        private BitmapImage ResizeImage(int NewWidth, int NewHeight, MemoryStream MemStream)
        {
            BitmapImage _reducedImage = new BitmapImage();

            _reducedImage.BeginInit();
            _reducedImage.CacheOption = BitmapCacheOption.None;
            _reducedImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
            _reducedImage.DecodePixelWidth = NewWidth ;
            _reducedImage.DecodePixelHeight = NewHeight;
            _reducedImage.StreamSource = MemStream;
            _reducedImage.Rotation = Rotation.Rotate0;
            _reducedImage.EndInit();

            return _reducedImage;
        }
    }
}
